package com.cloud.drore.eboss.supplier.mapper;

import com.cloud.drore.eboos.common.base.MyMapper;
import com.cloud.drore.eboos.common.entity.supplier.ProductRelation;
import com.cloud.drore.eboos.common.entity.supplier.vo.ConsumeAndPoiForDealer;
import com.cloud.drore.eboss.supplier.entity.po.ProductRelationPo;
import com.cloud.drore.eboss.supplier.entity.vo.ProductQueryVo;
import com.cloud.drore.eboss.supplier.entity.vo.ProductUnlimitedStockVo;
import com.cloud.drore.eboss.supplier.mapper.provider.ProductRelationProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

import java.util.List;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/9 10:21 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
public interface ProductRelationMapper extends MyMapper<ProductRelation> {

    static String  getTikcketUnlimitedStockSql = "SELECT  " +
            "  product.`id` AS common_product_id,product.`product_num`, " +
            "  proAsso.distributor_id,ticket.`term_of_validity_start_time`,ticket.`term_of_validity_end_time`  " +
            " FROM " +
            "  eboos_product product  " +
            "  INNER JOIN `eboos_ticket` ticket  " +
            "    ON product.product_id = ticket.id  " +
            "  INNER JOIN `eboos_distributor_product_association` proAsso  " +
            "    ON product.id = proAsso.`main_id`  " +
            "WHERE product.`stock_method` = 'UNLIMITED_STOCK' " +
            "   AND  product.`product_type` = 'TICKET_PRODUCT' " +
            "   AND  NOW() > ticket.`term_of_validity_start_time` " +
            "  AND NOW() < ticket.`term_of_validity_end_time` ";

    static String getCombinationUnlimitedStockSql = "SELECT  " +
            "  product.`id` AS common_product_id,product.`product_num`, " +
            "  proAsso.distributor_id,combination.`term_of_validity_start_time`,combination.`term_of_validity_end_time`  " +
            " FROM " +
            "  eboos_product product  " +
            "  INNER JOIN `eboos_combination_product` combination  " +
            "    ON product.product_id = combination.id  " +
            "  INNER JOIN `eboos_distributor_product_association` proAsso  " +
            "    ON product.id = proAsso.`main_id`  " +
            "WHERE product.`stock_method` = 'UNLIMITED_STOCK' " +
            "   AND  product.`product_type` = 'COMBINATION_PRODUCT' " +
            "   AND  NOW() > combination.`term_of_validity_start_time` " +
            "  AND NOW() < combination.`term_of_validity_end_time` ";


    String selectIdByTypeAndId = "select id from eboos_product where is_deleted =0 and product_type ='${productType}' and product_id = ${productId} limit 1";
    @Select("select * from eboos_product")

    List<ProductRelation> query();

    @SelectProvider(type = ProductRelationProvider.class, method = "findByParam")
    List<ProductRelation> findByParam(ProductRelationPo productRelation);

    @Select(selectIdByTypeAndId)
    Long selectIdByTypeAndId(@Param("productType") String productType, @Param("productId") Long productId);

    @Select("select id,product_id,merchant_id from eboos_product where is_deleted =0 and product_type ='${productType}' and product_id = ${productId} limit 1")
    ProductRelation selectProductByTypeAndId(@Param("productType") String productType, @Param("productId") Long productId);

    //根据供应商id查询所有产品编号
    @Select("select distinct product_num,product_name as name from eboos_product where is_deleted =0 and merchant_id ='${merchantId}' and product_type != 'CONBINATION_PRODUCT'")
    List<ProductQueryVo> selectProductIdBySupplierId(@Param("merchantId") Long merchantId);
    //查询所有产品编号
    @Select("select distinct product_num,product_name as name from eboos_product where is_deleted =0  and product_type != 'CONBINATION_PRODUCT'")
    List<ProductQueryVo> selectProductNum();
    @Select("select * from eboos_product where is_deleted =0 and product_num ='${proNum}' limit 1")
    ProductRelation selectProductByProNum(@Param("proNum") String proNum);

    @UpdateProvider(type = ProductRelationProvider.class, method = "updateProductByParam")
    int updateProductByParam(ProductRelation relation);

    @SelectProvider(type = ProductRelationProvider.class, method = "findPoiAndConsumeForDealer")
    ConsumeAndPoiForDealer findPoiAndConsumeForDealer(String productNum);

    //通过公共产品id拿到商户id的属性
    @Select("select merchant_id from eboos_product_consume_relation where product_id= ${id}")
    Long queryMerchantIdByProductId(@Param("id") Long id);

    //新增门票产品代码验重接口-杨亮
    @Select("select count(1) from eboos_product where product_code=#{productCode}")
    int countByCode(@Param("productCode") String productCode);

    @Select(getTikcketUnlimitedStockSql)
    List<ProductUnlimitedStockVo> getTikcketUnlimitedStock();

    @Select(getCombinationUnlimitedStockSql)
    List<ProductUnlimitedStockVo> getCombinationUnlimitedStock();

    @Select(" select product_name,product_code,product_num from eboos_product where is_deleted = '0' and status !='EXPIRED' order by created_date desc ")
    List<ProductRelation> findProductWithOutExpired();

}